Component({
  properties: {
    top: {
      type: null,
    },
    type: {
      type: null,
    },
    start: {
      type: null,
    },
    end: {
      type: null,
    },
  },
  data: {
    date_obj_arr: [],
    m_index: 8888,
    d_index: 9999,
  }, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      this.getData();
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {}, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {},

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },

  methods: {
    getData() {
      //获取日期数组
      let month_arr = this.getMonthsArr();
      //获取今天的日期和月份
      let now_month =
        new Date().getMonth() + 1 < 10
          ? "0" + (new Date().getMonth() + 1)
          : new Date().getMonth() + 1 + "";
      let current_day = new Date().getDate();
      month_arr.forEach((item) => {
        //获取天数
        let days = this.getMonthDay(item.year, item.month),
          day_arr = [],
          null_arr = [];
        //获取null个数
        let nulls = new Date(item.full).getDay();
        //处理天数数组
        for (let i = 0; i < days; i++) {
          let obj = {};
          obj["all_date"] = item.full + `/${i + 1}`;
          if (current_day == i + 1 && now_month == item.month) {
            obj["value"] = "今天";
          } else {
            obj["value"] = i + 1;
          }
          if (current_day > obj.value && now_month == item.month) {
            obj["isOld"] = "-1";
          } else if (current_day < obj.value && now_month == item.month) {
            obj["isOld"] = "1";
          } else {
            if (now_month == item.month) {
              obj["isOld"] = "0";
            } else {
              obj["isOld"] = "1";
            }
          }

          day_arr.push(obj);
        }
        //获取null数组
        for (let i = 0; i < nulls; i++) {
          null_arr.push("null");
        }
        item.nulla = null_arr; //加入数组null
        item.day = day_arr; //加入数组day_arr
      });
      this.setData({
        date_obj_arr: month_arr,
      });
      console.log(this.data.date_obj_arr,'date_obj_arr')
    },
    getMonthDay(year, month) {
      let d = new Date(year, month, 0);
      return d.getDate();
    },
    //获取month循环数组(获取动态12个月数组)
    getMonthsArr() {
      let dataArr = [],
        data = new Date(),
        year = data.getFullYear(),
        currentObj = {};
      currentObj.year = year.toString();
      currentObj.month =
        data.getMonth() + 1 < 10
          ? "0" + (data.getMonth() + 1)
          : data.getMonth() + 1 + "";
      currentObj.full = currentObj.year + "/" + currentObj.month;
      for (let i = 0; i < 11; i++) {
        data.setDate(1);
        data.setMonth(data.getMonth() + 1); //每次循环一次 月份值加1
        // let current_month = data.getMonth();
        let obj = {};
        obj.year = data.getFullYear().toString();
        obj.month =
          data.getMonth() + 1 < 10
            ? "0" + (data.getMonth() + 1)
            : data.getMonth() + 1 + "";
        obj.full = obj.year + "/" + obj.month;
        dataArr.push(obj);
      }
      dataArr.unshift(currentObj);
      return dataArr;
    },
    close() {
      this.triggerEvent("close", this.data.type);
    },
    chooseDate(e) {
      let index1 = e.currentTarget.dataset.index;
      let index2 = e.currentTarget.dataset.dayindex;
      let date = e.currentTarget.dataset.alldate;
      this.toggle(index1, index2);
      this.triggerEvent("timeChange", {
        type: this.data.type,
        date: date,
      });
    },
    toggle(index1, index2, date) {
      //切换选中
      if (this.data.m_index == index1 && this.data.d_index == index2) {
        return;
      } else {
        this.setData({
          m_index: index1,
          d_index: index2,
        });
      }
    },
  },
});
